import '../public/index'
import url from '../data/url'
import request from '../data/request'
import {tableConfig, header} from '../data/config'
import {getUrlParam} from '../data/util'
// 引入验证规则，只能输入字符和数字
import {deviceCode} from '../data/validate'
// 这个更改规则不变化之前的规则
const cusId = deviceCode

const base = url.base
const addYtj = '/admin/equipmentInfo/doAdd.json'

const app = new Vue({
  el: '#form',
  data: {
    equipType: '一体机',
    identifierCode: null,
    identifier: null,
    propertyArea: null,
    propertyAddress: null,
    propertyAreaList: [
      {val: 'hd', text: '华东'},
      {val: 'hx', text: '华西'},
      {val: 'hb', text: '华北'},
      {val: 'hn', text: '华南'},
      {val: 'hz', text: '华中'}
    ]
  },
  created() {
    this.valid()
  },
  methods: {
    // 获取页面类型pos还是一体机
    getPageType() {
      const type = getUrlParam('type')
      this.type = type
      let equipType
      switch (type) {
        case 'pos':
          equipType = 'pos'
          break
        case 'ytj':
          equipType = '一体机'
          break
      }
      this.equipType = equipType
    },
    valid() {
      const _form = this
      let data

      data = $('#form').serializeObject()

      let form_valid,
        $form = $('#form');

      $.validator.setDefaults({ // 只能放在validate规则配置之前

        // 提交事件
        submitHandler: () => {
          console.log('submit!');

          let loading_modal = layer.msg('提交中...', {
            icon: 16,
            time: 0
          });

          // console.log('data data:', data)
          $.ajax({
            type: "POST",
            url: base + addYtj,
            data: data,
            dataType: "json",
            headers: {
              myToken: Cookies.get('myToken')
            },
          })
            .done(function (res) {
              console.log('res====>', res);

              if (res.code === -1) {
                layer.msg(res.message, {
                  icon: 5
                });

                return
              }

              layer.msg(`添加设备成功`, {
                icon: 6
              });
              setTimeout(() => {
                window.location = './index.html?type=ytj'
              }, 1000)
            })
            .fail(function (res) {
              // console.log(res);

              // layer.msg(`${action}${ title }失败`, {
              //   icon: 5
              // });

              // console.log("error");
            })
            .always(function (res) {

              layer.close(loading_modal);
              form.isLock = false

            })

        }
      });

      let validOrderFailureTime
      validOrderFailureTime = _form.isApp
      // 验证
      form_valid = $form.validate({
        errorPlacement: function (error, element) {
          error.appendTo(element.parents('.col-xs-24'))
        },
        rules: {
          identifier: cusId,
          identifierCode: cusId
        }
      });
    },
    // 确认信息
    rUSure2Submit() {
      const device = this.equipType
      const deviceCode = this.identifier
      const submitForm = layer.confirm('', {
        title: '请确认设备新增信息',
        content: `您正在新增<strong style="color:red">${device}</strong>  <strong style="color:red" >${deviceCode ? deviceCode : ''}</strong> 请确认是否添加该设备`,
        btn: ['是', '否'] //按钮
      }, () => {
        app.valid()
        app.submitForm()
      }, () => {
        layer.close(submitForm)
      });
    },
    submitForm() {
      $('#form').submit()
    }
  }
})